package com.zhouyu.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

import java.lang.reflect.Constructor;

@Component
public class MyBeanPostProcessor implements BeanPostProcessor, InstantiationAwareBeanPostProcessor, SmartInstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, DestructionAwareBeanPostProcessor {

	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			System.out.println("### 实例化前 postProcessBeforeInstantiation userService");
		}
		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			System.out.println("### 实例化后 postProcessAfterInstantiation userService");
		}
		return true;
	}

	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			System.out.println("### 处理属性 postProcessProperties userService ");
		}
		return pvs;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			System.out.println("### 初始化前 postProcessBeforeInitialization userService");
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			System.out.println("### 初始化后 postProcessAfterInitialization userService");
		}
		return bean;
	}

	@Override
	public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			System.out.println("获取早期Bean引用 getEarlyBeanReference userService");
		}
		return bean;
	}

	@Override
	public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			System.out.println("推断构造函数 determineCandidateConstructors userService ");
		}
		return null;
	}

	@Override
	public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			System.out.println("自定义类型推测 predictBeanType userService");
		}
		return null;
	}

	@Override
	public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
		if ("userService".equals(beanName)) {
			System.out.println("### BeanDefinition的后置处理 postProcessMergedBeanDefinition userService");
		}
	}

	@Override
	public void resetBeanDefinition(String beanName) {
		if ("userService".equals(beanName)) {
			System.out.println("resetBeanDefinition userService");
		}
	}

	@Override
	public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
		if ("userService".equals(beanName)) {
			System.out.println("### 销毁前处理 postProcessBeforeDestruction userService");
		}
	}

	@Override
	public boolean requiresDestruction(Object bean) {
		if(bean instanceof UserService){
			System.out.println("requiresDestruction userService");
		}
		return true;
	}
}
